package com.ttl.bc.ic.stklocation.db;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.ttl.bc.ic.item.BCItemBean;
import com.ttl.bc.ic.stklocation.BCStkLocationBean;
import com.ttl.bc.ic.stklocation.BCStkLocationDAO;
import com.ttl.bc.ic.stklocation.BCStkLocationShow;
import com.ttl.common.util.CommonUtils;
import com.ttl.common.util.Page;

public class StkLocationBO {

	@SuppressWarnings("unchecked")
	public ArrayList find(HttpServletRequest request) {
		
		String	item = request.getParameter("item");
		int	maxResults = Integer.parseInt((String)request.getParameter("maxResults"));
						
		String hql = " FROM StkLocationBean s "+
				 	 " WHERE (s.bcItem.code like '%"+item+"%' or s.bcItem.name1 like '%"+item+"%') " +
				 	 		" and s.bcItem.name1 != null " +
				 	 		" and (s.bcWarehouse.code != null or s.bcWarehouse.code != '') " +
				 	 		" and s.bcWarehouse.code = '10' " +
				 	 		" and (s.bcShelf.code != null or s.bcShelf.code != '') " +
				 	 		" and s.bcShelf.code = '10' " +
				 	 		" ORDER BY s.bcItem.code";			
		
		//System.out.println("\n\nhql = "+hql);
		StkLocationDAO dao = new StkLocationDAO();		
		 		
		return (ArrayList)dao.find(hql, maxResults);
	}

	@SuppressWarnings("unchecked")
	public void setPage(HttpServletRequest request, String hql) {

		int pageNumber = 1, maxEntriesPerPage=50, lineNumber=1;		
		try {			
			pageNumber = Integer.parseInt(request.getParameter("page"));
		} catch (Exception e) {}
		
		if(request.getParameter("maxEntriesPerPage")!=null) {
			maxEntriesPerPage = Integer.parseInt(request.getParameter("maxEntriesPerPage"));			
		}
		if(request.getParameter("lineNumber")!=null) {
			request.setAttribute("lineNumber", request.getParameter("lineNumber"));
		}else {
			request.setAttribute("lineNumber", lineNumber);
		}
				
//		Page page = CommonUtils.getHibernatePage(hql, pageNumber, maxEntriesPerPage);				
		Page page = CommonUtils.getHibernateScrollableResultsPage(hql, pageNumber, maxEntriesPerPage);		
		
		request.setAttribute("maxEntriesPerPage", maxEntriesPerPage);
		request.setAttribute("allElement", page.getAllElement());
		
		ArrayList stkLocationList = new ArrayList();
//start
		if(page.getResultList()!=null && page.getResultList().size()>0) {
			
			String hql2 = "From BCStkLocationBean " +
					"WHERE whCode='10' and itemCode in ('"; 
			for (int i = 0; i < page.getResultList().size(); i++) {
				BCItemBean item = (BCItemBean)page.getResultList().get(i);
				hql2 += item.getCode()+"','";
				
				BCStkLocationShow show = new BCStkLocationShow();
				show.setItemCode(item.getCode());
				show.setItemName(item.getName1());
				show.setShelf10(0);	
				show.setShelf12(0);
				show.setShelf16(0);
				show.setShelf17(0);
				
				stkLocationList.add(show);
			}
			hql2 += "') order by itemCode ";
			//System.out.println(hql2);
			List list = new BCStkLocationDAO().find(hql2);
			
			for (int i = 0; i < stkLocationList.size(); i++) {
				
				BCStkLocationShow show = (BCStkLocationShow)stkLocationList.get(i);
				
				BigDecimal shelfTotal = new BigDecimal(0);
				MathContext mc = new MathContext(0, RoundingMode.DOWN);
				for (int j = 0; j < list.size(); j++) {
					BCStkLocationBean l = (BCStkLocationBean)list.get(j);
					if(show.getItemCode().equals(l.getItemCode())) {
						
						int shelfCode = Integer.valueOf(l.getShelfCode());
						switch (shelfCode) {
						case 10:	show.setShelf10(l.getQty());	break;
						case 12:	show.setShelf12(l.getQty());	break;
						case 16:	show.setShelf16(l.getQty());	break;
						case 17:	show.setShelf17(l.getQty());	break;
						}						
						
						BigDecimal qty = new BigDecimal(l.getQty(),mc);
						shelfTotal = shelfTotal.add(qty);
						//System.out.println(l.getQty()+" ==> "+ shelfTotal.floatValue());
					}
				}
				//show.setShelfTotal(show.getShelf10()+show.getShelf12()+show.getShelf16()+show.getShelf17());
				show.setShelfTotal(shelfTotal.floatValue());
				//System.out.println(show.getItemCode()+"="+show.getShelfTotal());
			}
		}
//end
		
		if(page.getAllElement()>0) {
			
			request.setAttribute("itemResults", stkLocationList );
			request.setAttribute("offset", (pageNumber-1)*maxEntriesPerPage);
			request.setAttribute("currentPage", page.getCurrentPage());
			
			if(page.isNextPage()==true) {
				request.setAttribute("nextPage", page.getCurrentPage()+1);
			}
			
			if(page.isPreviousPage()==true) {
				request.setAttribute("previousPage", page.getCurrentPage()-1);
			}
			
			if(page.isFirstPage()==true) {
				request.setAttribute("firstPage", 1);
			}		
			
			if(page.isLastPage()==true && page.getMaxEntriesPerPage()!=0) {
				if(page.getAllElement()%page.getMaxEntriesPerPage()==0) {
					request.setAttribute("lastPage", page.getAllElement()/page.getMaxEntriesPerPage());	
				}else {
					request.setAttribute("lastPage", page.getAllElement()/page.getMaxEntriesPerPage()+1);
				}						
			}
		}	
		
	}

}
